import java.util.ArrayList;


public class Board {

	private int numRows;
	private int numColumns;
	private int [][] data;
	
	
	//build a board with his size
	Board (int i, int j){
		numRows = i;
		numColumns = j;
		data = new int[i][j];
	}
	
	//return the number of rows
	public int getNumRows(){
		return numRows;
	}
	
	//return the number of columns
	public int getNumColumns(){
		return numColumns;
	}
	
	//set the i,j data
	public void setData(int i, int j, int value){
		data[i][j] = value;
	}
	
	//return the i,j data
	public int getData(int i, int j){
		return data[i][j];
	}
	
	
	
	//return true if we can move all boxes not in target square
	
	public boolean isOk () {
		boolean b = true;
		
		// Search for all the boxes
		for (int i=0;i<this.numRows;i++){
			for (int j=0;j<this.numColumns;j++){
				if (this.getData(i, j)==10){
					//a box in a corner 
					if ( (this.getData(i, j-1)==1 && this.getData(i-1, j)==1) 
							|| (this.getData(i-1, j)==1 && this.getData(i, j+1)==1)
							|| (this.getData(i, j+1)==1 && this.getData(i+1, j)==1)
							|| (this.getData(i+1, j)==1 && this.getData(i, j-1)==1)){
						b = false;
						
					}else 
					
					//tree boxes side by side along a wall
						if ( (this.getData(i, j+1)==10 && this.getData(i, j+2)==10 
								&& this.getData(i-1, j)==1 && this.getData(i-1, j+1)==1 && this.getData(i-1, j+2)==1) 
								|| (this.getData(i, j+1)==10 && this.getData(i, j+2)==10
										&& this.getData(i+1, j)==1 && this.getData(i+1, j+1)==1 && this.getData(i+1, j+2)==1)
										|| (this.getData(i+1, j)==10 && this.getData(i+2, j)==10
												&& this.getData(i, j-1)==1 && this.getData(i+1, j-1)==1 && this.getData(i+2, j-1)==1)
												|| (this.getData(i+1, j)==10 && this.getData(i+2, j)==10
														&& this.getData(i, j+1)==1 && this.getData(i+1, j+1)==1 && this.getData(i+2, j+1)==1)
												){
							b=false;						
						}
				}
			}
		}
		return b;
	}
	
	
	//return the path between the player square and the square in argument if it's reachable, and "null" if it's not
	public String pathTo(int iGoal, int jGoal){
		String s = null;
		int iPlayer=0;
		int jPlayer=0;
		ArrayList<int[]> lLastSquares = new ArrayList<int[]>(); //all squares covered
//		ArrayList<int> lMovesPlayer = nez ArrayList<int[]>(); //
		
		//search the square of the player
		for (int i=0;i<this.numRows;i++){
			for (int j=0;j<this.numColumns;j++){
				if (this.getData(i, j)==20 || this.getData(i, j)==22){
					iPlayer = i;
					jPlayer =j;
				}
				if (iPlayer!=0) break;
			}
			if (iPlayer!=0) break;
		}
		
		while (true){
			int smallerDistance = 10000;
			int effectivMov;
			if (this.getData(iPlayer-1, jPlayer)%10==0){
				int x = iGoal-(iPlayer-1);
				int y = jGoal-jPlayer;
				int dist = x*x+y*y;
				if (dist<smallerDistance){
					smallerDistance = dist;
					effectivMov = 0;
				}
			} 
			if  (this.getData(iPlayer+1, jPlayer)%10==0){
				int x = iGoal-(iPlayer+1);
				int y = jGoal-jPlayer;
				int dist = x*x+y*y;
				if (dist<smallerDistance){
					smallerDistance = dist;
					effectivMov = 1;
				}
			} 
			if (this.getData(iPlayer, jPlayer-1)%10==0){
				int x = iGoal-iPlayer;
				int y = jGoal-(jPlayer-1);
				int dist = x*x+y*y;
				if (dist<smallerDistance){
					smallerDistance = dist;
					effectivMov = 2;
				}
			}
			if (this.getData(iPlayer, jPlayer+1)%10==0){
					int x = iGoal-iPlayer;
					int y = jGoal-(jPlayer+1);
					int dist = x*x+y*y;
					if (dist<smallerDistance){
						smallerDistance = dist;
						effectivMov = 3;
					}
				} 
			break;
			
		}
		
		
		
		
		return s;
	}
 	
}
